#tag Class
Protected Class RandomGuy
Implements ArtificialPlayer
	#tag Method, Flags = &h0
		Sub Calculate()
		  dim xy() as coordinates
		  dim pos as Coordinates
		  dim r as new Random
		  dim i as integer
		  dim dbg as string
		  dim theTile as board.Tile
		  dim noOfTiles as Integer
		  
		  
		  'msgbox "AI-Player " + player.GetName
		  
		  
		  
		  theTile =  player.GetGame.GetNextTile
		  
		  if theTile <> nil then
		    
		    for i = 0 to r.inRange(0,3)
		      theTile.rotateClockwise
		    next
		    
		    
		    xy = player.GetGame.GetPossibleCoordinatesForTile
		    
		    while ubound(xy) = -1
		      theTile.rotateClockwise
		      i = i + 1
		      xy = player.GetGame.GetPossibleCoordinatesForTile
		    wend
		    
		    noOfTiles = ubound(xy)
		    
		    pos = xy(r.InRange(0,noOfTiles))
		    
		    call player.AddTileAtPosition(theTile, pos)
		    
		    dbg = player.getName + " addTile " + theTile.GetUid + " at position " + str(pos.x) + "/" + str(pos.y)
		    
		    if board.GetPositionIsOccupieableTown(pos, player) = true or board.GetPositionIsOccupieableStreet(pos, player) then
		      if player.HasGamepieces = true then
		        if r.InRange(0,1) = 1 then //50:50 chance to place gamepiece if gamepieces are left
		          dim possibleSlots() as board.slot
		          possibleSlots = player.GetGame.GetCurrentTile.grid.GetPossibleSlots
		          
		          //if slots are existing - take the first one
		          if ubound(possibleSlots) > -1 then
		            for i = 0 to ubound (possibleSlots)
		              if possibleSlots(i).GetType <> "Grassland" and possibleSlots(i).GetType <> "undefined" then
		                player.Occupy possibleSlots(i).GetPosition
		                dbg = dbg + " occupy " + possibleSlots(i).GetType
		                exit for
		              end if
		            next
		          end if
		          
		        end if
		      else
		        dbg = dbg + " " + player.GetName + " has no gamepieces to place"
		      end if
		    else
		      dbg = dbg + " --> position " + str(pos.x) + "/" + str(pos.y) + " is not occupyable"
		    end if
		    app.Dbg = app.Dbg + dbg + EndOfLine
		    app.Dbg = app.Dbg + player.GetName + " Score: " + str(player.getscore) + EndOfLine
		    
		    player.EndRound
		    
		  else
		    player.EndGame
		  end if
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub Constructor(p as game . Player)
		  self.player = p
		  game = p.GetGame
		  board = game.GetBoard
		End Sub
	#tag EndMethod


	#tag Property, Flags = &h0
		player As game.Player
	#tag EndProperty

	#tag Property, Flags = &h0
		game As game.concretegame
	#tag EndProperty

	#tag Property, Flags = &h0
		board As board.GameBoard
	#tag EndProperty


	#tag ViewBehavior
		#tag ViewProperty
			Name="Name"
			Visible=true
			Group="ID"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Index"
			Visible=true
			Group="ID"
			InitialValue="-2147483648"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Super"
			Visible=true
			Group="ID"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Left"
			Visible=true
			Group="Position"
			InitialValue="0"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Top"
			Visible=true
			Group="Position"
			InitialValue="0"
			InheritedFrom="Object"
		#tag EndViewProperty
	#tag EndViewBehavior
End Class
#tag EndClass
